package com.base.cn.platform.os.controller.user.binding;

import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.email.EmailSendUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.common.utils.sms.SmsSendUtils;
import com.base.cn.platform.os.entity.user.user.CusUser;
import com.base.cn.platform.os.service.manage.config.SysConfigBiz;
import com.base.cn.platform.os.service.user.user.CusUserBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;
import java.util.TreeMap;

/**
 * 用户手机或邮箱号绑定Controller接口
 *
 * @author s.li
 * @create 2018-06-05-15:58
 */
@RestController
@RequestMapping("/manage/user")
public class UserMobileOrEmailBindingController extends BaseController {

    @Autowired
    private CusUserBiz cusUserBiz;
    @Autowired
    private SmsSendUtils smsSendUtils;
    @Autowired
    private SysConfigBiz sysConfigBiz;
    @Autowired
    private EmailSendUtils emailSendUtils;

    /**
     * 验证原绑定的手机号(如果没有绑定过的手机号，将用不到该接口)
     * @param mobile 手机号
     * @param mobileCode 手机验证码
     * @return Map<String,Object>
     */
    @RequestMapping("/verificationOldMobile")
    public Map<String,Object> verificationOldMobile(@RequestParam("mobile") String mobile,
                                                    @RequestParam("mobileCode") String mobileCode){
        if(StringUtils.isEmpty(mobile)){
            return ResultUtil.ERROR("请填写原绑定手机号");
        }
        if(!StringUtils.isMobile(mobile)){
            return ResultUtil.ERROR("请填写正确手机号");
        }
        if(StringUtils.isEmpty(mobileCode)){
            return ResultUtil.ERROR("请填写手机验证码");
        }
        TreeMap<String,String> configMap = sysConfigBiz.getSysConfigContext(ConfigName.SMS);
        smsSendUtils.setSmsConfig(configMap);
        boolean isOk = smsSendUtils.validateUnBindingCode(mobile,mobileCode);
        if(!isOk){
            return ResultUtil.ERROR("手机验证码错误");
        }
        return ResultUtil.SUCCESS("验证成功",mobileCode);
    }

    /**
     * 绑定手机号
     * @param userId 用户ID
     * @param mobile 新的手机号
     * @param mobileCode 新手机号的验证码
     * @param oldMobileCode 原手机号的验证码
     * @return Map<String,Object>
     */
    @RequestMapping("/bindingUserMobile")
    public Map<String,Object> bindingUserMobile(@RequestParam("userId") BigDecimal userId,
                                                @RequestParam("mobile") String mobile,
                                                @RequestParam("mobileCode") String mobileCode,
                                                @RequestParam(value = "oldMobileCode",required = false) String oldMobileCode){
        CusUser cusUser = cusUserBiz.findById(userId);
        if(ObjectUtils.isEmpty(cusUser)){
            return ResultUtil.ERROR("用户不存在，手机号绑定失败");
        }
        if(StringUtils.isEmpty(mobile)){
            return ResultUtil.ERROR("请填写新手机号");
        }
        if(!StringUtils.isMobile(mobile)){
            return ResultUtil.ERROR("请填写正确的手机号");
        }
        if(StringUtils.isEmpty(mobileCode)){
            return ResultUtil.ERROR("请填写新手机号短信验证码");
        }
        TreeMap<String,String> configMap = sysConfigBiz.getSysConfigContext(ConfigName.SMS);
        smsSendUtils.setSmsConfig(configMap);
        boolean oldMobileStatus = false;
        //如果有绑定过手机号
        if(StringUtils.isNotEmpty(cusUser.getMobile())){
            if(cusUser.getMobile().equals(mobile)){
                return ResultUtil.ERROR("该手机号已绑定");
            }
            if(StringUtils.isEmpty(oldMobileCode)){
                return ResultUtil.ERROR("请先验证原绑定的手机号");
            }
            //验证原绑定的手机号
            boolean s = smsSendUtils.validateUnBindingCode(cusUser.getMobile(),oldMobileCode);
            if(!s){
                return ResultUtil.ERROR("请先验证原绑定的手机号");
            }
            oldMobileStatus = s;
        }
        boolean isOk = smsSendUtils.validateBindingCode(mobile,mobileCode);
        if(!isOk){
            return ResultUtil.ERROR("新手机号验证码错误");
        }
        String oldMobile = cusUser.getMobile();
        //更新手机号
        cusUser.setMobile(mobile);
        cusUser.setMobileIsavalible(1);
        cusUserBiz.updateById(cusUser,null);
        //删除原绑定手机号的验证码缓存
        if(oldMobileStatus){
            smsSendUtils.removeUnBindingCode(oldMobile);
        }
        //删除新手机号验证码的缓存
        smsSendUtils.removeBindingCode(mobile);
        return ResultUtil.SUCCESS("手机号绑定成功");
    }

    //===========================================绑定邮箱号，开始=====================
    /**
     * 验证原绑定的邮箱号(如果没有绑定过的邮箱号，将用不到该接口)
     * @param email 邮箱号
     * @param emailCode 邮箱验证码
     * @return Map<String,Object>
     */
    @RequestMapping("/verificationOldEmail")
    public Map<String,Object> verificationOldEmail(@RequestParam("email") String email,
                                                    @RequestParam("mobileCode") String emailCode){
        if(StringUtils.isEmpty(email)){
            return ResultUtil.ERROR("请填写原绑定邮箱号");
        }
        if(!StringUtils.isEmail(email)){
            return ResultUtil.ERROR("请填写正确邮箱号");
        }
        if(StringUtils.isEmpty(emailCode)){
            return ResultUtil.ERROR("请填写邮箱验证码");
        }
        TreeMap<String,String> emailConfigMap = sysConfigBiz.getSysConfigContext(ConfigName.EMAIL);
        emailSendUtils.setEmailConfig(emailConfigMap);
        TreeMap<String,String> infoConfigMap = sysConfigBiz.getSysConfigContext(ConfigName.INFO);
        emailSendUtils.setInfoConfigMap(infoConfigMap);
        boolean isOk = emailSendUtils.validateUnBindingCode(email,emailCode);
        if(!isOk){
            return ResultUtil.ERROR("邮箱验证码错误");
        }
        return ResultUtil.SUCCESS("验证成功",emailCode);
    }

    /**
     * 绑定邮箱号
     * @param userId 用户ID
     * @param email 新的邮箱号
     * @param emailCode 新邮箱号的验证码
     * @param oldEmailCode 原邮箱号的验证码
     * @return Map<String,Object>
     */
    @RequestMapping("/bindingUserEmail")
    public Map<String,Object> bindingUserEmail(@RequestParam("userId") BigDecimal userId,
                                                @RequestParam("email") String email,
                                                @RequestParam("emailCode") String emailCode,
                                                @RequestParam(value = "oldEmailCode",required = false) String oldEmailCode){
        CusUser cusUser = cusUserBiz.findById(userId);
        if(ObjectUtils.isEmpty(cusUser)){
            return ResultUtil.ERROR("用户不存在，邮箱号绑定失败");
        }
        if(StringUtils.isEmpty(email)){
            return ResultUtil.ERROR("请填写新邮箱号");
        }
        if(!StringUtils.isEmail(email)){
            return ResultUtil.ERROR("请填写正确的手机号");
        }
        if(StringUtils.isEmpty(emailCode)){
            return ResultUtil.ERROR("请填写新邮箱号验证码");
        }

        TreeMap<String,String> emailConfigMap = sysConfigBiz.getSysConfigContext(ConfigName.EMAIL);
        emailSendUtils.setEmailConfig(emailConfigMap);
        TreeMap<String,String> infoConfigMap = sysConfigBiz.getSysConfigContext(ConfigName.INFO);
        emailSendUtils.setInfoConfigMap(infoConfigMap);

        boolean oldEmailStatus = false;
        //如果有绑定过邮箱号
        if(StringUtils.isNotEmpty(cusUser.getEmail())){
            if(cusUser.getMobile().equals(email)){
                return ResultUtil.ERROR("该邮箱号已绑定");
            }
            if(StringUtils.isEmpty(oldEmailCode)){
                return ResultUtil.ERROR("请先验证原绑定的邮箱号");
            }
            //验证原绑定的邮箱号
            boolean s = emailSendUtils.validateUnBindingCode(cusUser.getEmail(),oldEmailCode);
            if(!s){
                return ResultUtil.ERROR("请先验证原绑定的邮箱号");
            }
            oldEmailStatus = s;
        }
        boolean isOk = emailSendUtils.validateBindingCode(email,emailCode);
        if(!isOk){
            return ResultUtil.ERROR("新邮箱号验证码错误");
        }
        String oldEmail = cusUser.getEmail();
        //更新邮箱号
        cusUser.setEmail(email);
        cusUser.setEmailIsavalible(1);
        cusUserBiz.updateById(cusUser,null);
        //删除原绑定邮箱号的验证码缓存
        if(oldEmailStatus){
            emailSendUtils.removeUnBindingCode(oldEmail);
        }
        //删除新邮箱号验证码的缓存
        emailSendUtils.removeBindingCode(email);
        return ResultUtil.SUCCESS("邮箱号绑定成功");
    }
    //===========================================绑定邮箱号，结束=====================
}
